package com.luxsoft.em2.replication.central.consumers;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Topic;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.jms.core.MessagePostProcessor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.luxsoft.em2.EM2ServiceLocator;
import com.luxsoft.sw3.replica.EntityLog;

/**
 * Entidad para importar mediante JMS Altas de clientes en las sucursales
 * 
 * @author Ruben Cancino 
 *
 */
@Service(value="clienteCentralHandler")
public class ClienteCentralHandler extends AbstractConsumer implements InitializingBean{
	
	
	
	
	@Autowired
	@Qualifier("clientesTopic")	
	private Topic clientesTopic;
	
	@Override
	public void afterReplicate(final EntityLog log) {
		EM2ServiceLocator.getJmsTemplate().convertAndSend(clientesTopic, log,new MessagePostProcessor() {
			public Message postProcessMessage(Message message) throws JMSException {
				message.setStringProperty("SUCURSAL_ORIGEN", log.getSucursalOrigen());
				return message;
			}
		});
		logger.info("Cliente enviado a topico para actualizacion en sucursales Topico: "+clientesTopic);
	}
	
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(clientesTopic,"No se ha registrado un javax.jms.Topic para clientes modificados");
		logger.info("Topic para clientes: "+clientesTopic.getTopicName());
		
	}
	
	public void setClientesTopic(Topic clientesTopic) {
		this.clientesTopic = clientesTopic;
	}

}
